// ============================================================================
//
// LCP-net framework by Pierre CHATEL chatelp@gmail.com
// Distributed under GPLv3
// Copyright (C) THALES Land & Joint Systems, 2008, 2009. All rights reserved
//
// ============================================================================

/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;

import com.thalesgroup.preferenceFramework.models.LCPnet.CLPTLine;
import com.thalesgroup.preferenceFramework.models.LCPnet.LNodeValue;
import com.thalesgroup.preferenceFramework.models.LCPnet.LNodeValueUtility;
import com.thalesgroup.preferenceFramework.models.LCPnet.exceptions.LCPnetCombinationException;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.CLPTFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.CLPTLineFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.Fragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.FragmentsPackage;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LNodeValueFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LNodeValueUtilityFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.impl.CLPTLineImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>CLPT Line Fragment</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.CLPTLineFragmentImpl#getFragmentName <em>Fragment Name</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.CLPTLineFragmentImpl#isAntiFragment <em>Anti Fragment</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.CLPTLineFragmentImpl#getBaseCLPTLine <em>Base CLPT Line</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class CLPTLineFragmentImpl extends CLPTLineImpl implements CLPTLineFragment {
	/**
	 * The default value of the '{@link #getFragmentName() <em>Fragment Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFragmentName()
	 * @generated
	 * @ordered
	 */
	protected static final String FRAGMENT_NAME_EDEFAULT = "";
	/**
	 * The cached value of the '{@link #getFragmentName() <em>Fragment Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFragmentName()
	 * @generated
	 * @ordered
	 */
	protected String fragmentName = FRAGMENT_NAME_EDEFAULT;
	/**
	 * The default value of the '{@link #isAntiFragment() <em>Anti Fragment</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAntiFragment()
	 * @generated
	 * @ordered
	 */
	protected static final boolean ANTI_FRAGMENT_EDEFAULT = false;
	/**
	 * The cached value of the '{@link #isAntiFragment() <em>Anti Fragment</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAntiFragment()
	 * @generated
	 * @ordered
	 */
	protected boolean antiFragment = ANTI_FRAGMENT_EDEFAULT;
	/**
	 * The cached value of the '{@link #getBaseCLPTLine() <em>Base CLPT Line</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBaseCLPTLine()
	 * @generated
	 * @ordered
	 */
	protected CLPTLine baseCLPTLine;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected CLPTLineFragmentImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return FragmentsPackage.Literals.CLPT_LINE_FRAGMENT;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getFragmentName() {
		return fragmentName;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setFragmentName(String newFragmentName) {
		String oldFragmentName = fragmentName;
		fragmentName = newFragmentName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.CLPT_LINE_FRAGMENT__FRAGMENT_NAME, oldFragmentName, fragmentName));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isAntiFragment() {
		return antiFragment;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setAntiFragment(boolean newAntiFragment) {
		boolean oldAntiFragment = antiFragment;
		antiFragment = newAntiFragment;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.CLPT_LINE_FRAGMENT__ANTI_FRAGMENT, oldAntiFragment, antiFragment));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public CLPTLine getBaseCLPTLine() {
		if (baseCLPTLine != null && baseCLPTLine.eIsProxy()) {
			InternalEObject oldBaseCLPTLine = (InternalEObject)baseCLPTLine;
			baseCLPTLine = (CLPTLine)eResolveProxy(oldBaseCLPTLine);
			if (baseCLPTLine != oldBaseCLPTLine) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, FragmentsPackage.CLPT_LINE_FRAGMENT__BASE_CLPT_LINE, oldBaseCLPTLine, baseCLPTLine));
			}
		}
		return baseCLPTLine;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public CLPTLine basicGetBaseCLPTLine() {
		return baseCLPTLine;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBaseCLPTLine(CLPTLine newBaseCLPTLine) {
		CLPTLine oldBaseCLPTLine = baseCLPTLine;
		baseCLPTLine = newBaseCLPTLine;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.CLPT_LINE_FRAGMENT__BASE_CLPT_LINE, oldBaseCLPTLine, baseCLPTLine));
	}

	public void applyFragment(EObject context) throws LCPnetCombinationException {
		if(context instanceof CLPTFragment) {
			//Fragment contained in context --> apply fragment

			CLPTFragment callingCLPTfragment = (CLPTFragment) context;
			CLPTLine baseClptLine = this.getBaseCLPTLine();
			
			if(this.isAntiFragment()) {
				EList<CLPTLine> baseClptLines = callingCLPTfragment.getBaseCLPT().getLines();
				baseClptLines.remove(baseClptLine);
				return;
			}
			
			//Warning: If it's an antiFragment, the following code will not be executed

			if(this.getParentValues() != null) {
				EList<LNodeValue> parentValues = this.getParentValues();
				//Avoid list concurrent modifications
				LNodeValue[] parentValues_array = parentValues.toArray(new LNodeValue[]{});
				
				for (LNodeValue nodeValue : parentValues_array) {
					if(nodeValue instanceof LNodeValueFragment) {
						//parent value fragment --> apply fragment
						((LNodeValueFragment) nodeValue).applyFragment(this);
					} else {
						//new parent value --> add to base clptLine
						baseClptLine.getParentValues().add(nodeValue);
					}
				}
			}
			
			if(this.getUtility() != null) {
				EList<LNodeValueUtility> lnodeValueUtilities = this.getUtility();
				//Avoid list concurrent modifications
				LNodeValueUtility[] lnodeValueUtilities_array = lnodeValueUtilities.toArray(new LNodeValueUtility[]{});
				
				for (LNodeValueUtility lNodeValueUtility : lnodeValueUtilities_array) {
					if(lNodeValueUtility instanceof LNodeValueUtilityFragment) {
						//LNodeValueUtility fragment --> apply fragment
						((LNodeValueUtilityFragment) lNodeValueUtility).applyFragment(this);
					} else {
						//new LNodeValueUtility --> add to base clptLine
						baseClptLine.getUtility().add(lNodeValueUtility);
					}
				}
			}
		} else {
			throw new LCPnetCombinationException("Combination error: unexpected calling context for CLPTLine fragment");
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case FragmentsPackage.CLPT_LINE_FRAGMENT__FRAGMENT_NAME:
				return getFragmentName();
			case FragmentsPackage.CLPT_LINE_FRAGMENT__ANTI_FRAGMENT:
				return isAntiFragment() ? Boolean.TRUE : Boolean.FALSE;
			case FragmentsPackage.CLPT_LINE_FRAGMENT__BASE_CLPT_LINE:
				if (resolve) return getBaseCLPTLine();
				return basicGetBaseCLPTLine();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case FragmentsPackage.CLPT_LINE_FRAGMENT__FRAGMENT_NAME:
				setFragmentName((String)newValue);
				return;
			case FragmentsPackage.CLPT_LINE_FRAGMENT__ANTI_FRAGMENT:
				setAntiFragment(((Boolean)newValue).booleanValue());
				return;
			case FragmentsPackage.CLPT_LINE_FRAGMENT__BASE_CLPT_LINE:
				setBaseCLPTLine((CLPTLine)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case FragmentsPackage.CLPT_LINE_FRAGMENT__FRAGMENT_NAME:
				setFragmentName(FRAGMENT_NAME_EDEFAULT);
				return;
			case FragmentsPackage.CLPT_LINE_FRAGMENT__ANTI_FRAGMENT:
				setAntiFragment(ANTI_FRAGMENT_EDEFAULT);
				return;
			case FragmentsPackage.CLPT_LINE_FRAGMENT__BASE_CLPT_LINE:
				setBaseCLPTLine((CLPTLine)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case FragmentsPackage.CLPT_LINE_FRAGMENT__FRAGMENT_NAME:
				return FRAGMENT_NAME_EDEFAULT == null ? fragmentName != null : !FRAGMENT_NAME_EDEFAULT.equals(fragmentName);
			case FragmentsPackage.CLPT_LINE_FRAGMENT__ANTI_FRAGMENT:
				return antiFragment != ANTI_FRAGMENT_EDEFAULT;
			case FragmentsPackage.CLPT_LINE_FRAGMENT__BASE_CLPT_LINE:
				return baseCLPTLine != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eBaseStructuralFeatureID(int derivedFeatureID, Class<?> baseClass) {
		if (baseClass == Fragment.class) {
			switch (derivedFeatureID) {
				case FragmentsPackage.CLPT_LINE_FRAGMENT__FRAGMENT_NAME: return FragmentsPackage.FRAGMENT__FRAGMENT_NAME;
				case FragmentsPackage.CLPT_LINE_FRAGMENT__ANTI_FRAGMENT: return FragmentsPackage.FRAGMENT__ANTI_FRAGMENT;
				default: return -1;
			}
		}
		return super.eBaseStructuralFeatureID(derivedFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedStructuralFeatureID(int baseFeatureID, Class<?> baseClass) {
		if (baseClass == Fragment.class) {
			switch (baseFeatureID) {
				case FragmentsPackage.FRAGMENT__FRAGMENT_NAME: return FragmentsPackage.CLPT_LINE_FRAGMENT__FRAGMENT_NAME;
				case FragmentsPackage.FRAGMENT__ANTI_FRAGMENT: return FragmentsPackage.CLPT_LINE_FRAGMENT__ANTI_FRAGMENT;
				default: return -1;
			}
		}
		return super.eDerivedStructuralFeatureID(baseFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (fragmentName: ");
		result.append(fragmentName);
		result.append(", antiFragment: ");
		result.append(antiFragment);
		result.append(')');
		return result.toString();
	}

} //CLPTLineFragmentImpl
